<?php
namespace app\common\traits;

use think\Paginator;
use think\response\Json;

trait ApiPaginatorTrait
{
    /**
     * 标准化API分页响应
     * @param Paginator $paginator
     * @param array $extra 额外数据
     * @return Json
     */
    protected function apiPaginate($paginator, array $extra = []): Json
    {
        $data = array_merge([
            'code' => 200000,
            'data' => [
                'list'       => $paginator->items(),
                'pagination' => [
                    'total'        => $paginator->total(),
                    'per_page'     => $paginator->listRows(),
                    'current_page' => $paginator->currentPage(),
                    'last_page'    => $paginator->lastPage(),
                ]
            ]
        ], $extra);

        return json($data);
    }
}